package com.bupt.liutong.admin.shop.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bupt.liutong.admin.core.base.BaseAction;
import com.bupt.liutong.admin.shop.bo.CourseBo;
import com.bupt.liutong.admin.shop.form.ProdForm;

public class CourseAction extends BaseAction {
	private CourseBo courseBo;
	
	public ActionForward updateIntro(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("shop_course_intro", courseBo.getIntro());
		return mapping.findForward("updateIntroSuccess");
	}
	
	public ActionForward updateIntroDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		courseBo.updateIntro(request);
		return this.updateIntro(mapping, form, request, response);
	}
	
	public ActionForward getCoursesByPage(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		request.setAttribute("prodFormList", courseBo.getCoursesByPage(prodForm));
		request.setAttribute("enableMap", ctm.get("ifDisp"));
		request.setAttribute("hotMap", ctm.get("ifNewCourse"));
		putPageInfo(request, prodForm);
		return mapping.findForward("getCoursesByPageSuccess");
	}
	
	public ActionForward insertCourse(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("prodTypeFormList", courseBo.getTypes());
		request.setAttribute("attrFormList", courseBo.getAttrs());
		return mapping.findForward("insertCourseSuccess");
	}
	
	public ActionForward insertCourseDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		courseBo.insertCourse(prodForm, request);
		return mapping.findForward("insertCourseDoSuccess");
	}
	
	public ActionForward updateCourse(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		request.setAttribute("prodForm", courseBo.getCourseById(prodForm));
		request.setAttribute("prodTypeFormList", courseBo.getTypes());
		request.setAttribute("photoFormList", courseBo.getCoursePhotosById(prodForm));
		request.setAttribute("attrFormList", courseBo.getAttrs());
		request.setAttribute("attrValMap", courseBo.getAttrVals(prodForm));
		return mapping.findForward("updateCourseSuccess");
	}
	
	public ActionForward updateCourseDo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ProdForm prodForm = (ProdForm) form;
		courseBo.updateCourse(prodForm, request);
		return mapping.findForward("updateCourseDoSuccess");
	}
	
	public ActionForward deleteCourses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		courseBo.deleteCourses(ids);
		return mapping.findForward("deleteCoursesSuccess");
	}
	
	public ActionForward enableCourses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		courseBo.enableCourses(ids);
		return mapping.findForward("enableCoursesSuccess");
	}
	
	public ActionForward disableCourses(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		courseBo.disableCourses(ids);
		return mapping.findForward("disableCoursesSuccess");
	}
	
	public ActionForward updateCourseHot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		courseBo.updateCourseHot(ids);
		return mapping.findForward("deleteCoursesSuccess");
	}
	
	public ActionForward updateCourseUnHot(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String[] ids = request.getParameterValues("id");
		courseBo.updateCourseUnHot(ids);
		return mapping.findForward("deleteCoursesSuccess");
	}

	public void setCourseBo(CourseBo courseBo) {
		this.courseBo = courseBo;
	}
}
